package MultiPong;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

public class MultiPongTest extends Canvas implements Runnable,
		MouseMotionListener, MouseListener {
	private static final long serialVersionUID = 1L;

	private boolean running;
	private long lastFrameTime;
	private Graphics graphics;
	private BufferStrategy buffer;

	private static int xpos = 350;
	private static int xpos_bal = 400;
	private static int ypos_bal = 400;
	private int ypos = 350;
	private int fps;
	private long lastFps;
	private long vastfps;
	
	private HoofdPaneel hoofdpaneel = HoofdPaneel.getInstance();
	
	public int getXpos(){
		int x = this.xpos;   
		return x;
	}
	public int getXpos_bal(){
		int x_bal = this.xpos_bal;   // getter xposbal door wim
		return x_bal;
	}
	public int getYpos_bal(){
		int y_bal = this.ypos_bal;   // getter yposbal door wim 
		return y_bal;
	}
	

	private static MultiPongTest instance;

	public static MultiPongTest getInstance() {
		if (instance == null) {
			instance = new MultiPongTest();
		}
		return instance;
	}

	private MultiPongTest() {
		this.setSize(new Dimension(800, 800));
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
	}

	public void addNotify() {
		super.addNotify();
		this.createBufferStrategy(2);
		this.buffer = this.getBufferStrategy();
		this.requestFocus();
		this.start();
	}

	public void start() {
		this.running = true;
		Thread thread = new Thread(this);
		thread.setPriority(Thread.MAX_PRIORITY);
		thread.start();
	}

	public void stop() {
		this.running = false;
	}

	@Override
	public void run() {
		lastFps = System.currentTimeMillis();
		while (this.running) {
			this.tick(this.getDelta());
			this.render();
		}
	}

	public void render() {
		graphics = buffer.getDrawGraphics();
		graphics.clearRect(0, 0, 800, 800);
		graphics.setColor(Color.white);
		graphics.fillRect(0, 0, 800, 800);
		// switch case aan de hand van playernummer
		graphics.setColor(Color.red);
		/*
		 * switch(playernummer){ case 1 : graphics.fillRect(xpos, 10, 100, 10);
		 * //Boven braek; case 2 : graphics.fillRect(xpos, 780, 100, 10);
		 * //Onder braek; case 3 : graphics.fillRect(10, ypos, 10, 100); //Links
		 * braek; case 4 : graphics.fillRect(780, ypos, 10, 100); //Links braek;
		 * } einde van het switch statement
		 */
		
		
		graphics.fillRect(Integer.parseInt(hoofdpaneel.getXpos()),10, 100, 10); // Boven
		graphics.fillRect(xpos, 780, 100, 10); // Onder
		//graphics.fillRect(10, ypos, 10, 100); // Links
		//graphics.fillRect(780, ypos, 10, 100); // Links

		graphics.setColor(Color.black);
		graphics.fillRect(0, 0, 800, 10);
		graphics.fillRect(0, 0, 10, 800);
		graphics.fillRect(790, 0, 10, 800);
		graphics.fillRect(0, 790, 800, 10);
		graphics.fillRect(0, 0, 50, 20);
		graphics.fillRect(0, 0, 20, 50);
		graphics.fillRect(750, 0, 50, 20);
		graphics.fillRect(780, 0, 20, 50);
		graphics.fillRect(0, 780, 50, 20);
		graphics.fillRect(0, 750, 20, 50);
		graphics.fillRect(780, 750, 20, 50);
		graphics.fillRect(750, 780, 50, 20);
		
		//tekenen van de bal
		graphics.setColor(Color.black);
		graphics.fillOval(Integer.parseInt(hoofdpaneel.getXpos_bal()), Integer.parseInt(hoofdpaneel.getYpos_bal()), 22, 22); //eerste bal groen andere zwart voor omlijning
		graphics.setColor(Color.green);
		graphics.fillOval(Integer.parseInt(hoofdpaneel.getXpos_bal())+1, Integer.parseInt(hoofdpaneel.getYpos_bal())+1, 20, 20);
		
		
		graphics.setColor(Color.white);
		graphics.drawString(""+vastfps,770,10);

		// for(Player player :players){
		// player.render();
		// }
		// ball.render();
		Toolkit.getDefaultToolkit().sync();
		if (!buffer.contentsLost()) {
			buffer.show();
			if (graphics != null) {
				graphics.dispose();
			}
		}
	}

	public void tick(int delta) {
		updateFPS();

		// for (Player player : players) {
		// player.tick(delta);
		// }
		// ball.tick(delta);
	}

	private int getDelta() {
		long currentTime = System.currentTimeMillis();
		int delta = (int) (currentTime - lastFrameTime);
		lastFrameTime = System.currentTimeMillis();
		return delta;
	}

	public void updateFPS() {
		if (System.currentTimeMillis() - lastFps > 1000) {
			//System.out.println("FPS: " + fps);
			vastfps = fps;
			fps = 0;
			lastFps += 1000;
		}
		fps++;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		xpos = e.getX() - 50;
		ypos = e.getY() - 50;
		if (xpos <= 50) {
			xpos = 50;
		}
		if (xpos >= 650) {
			xpos = 650;
		}
		if (ypos <= 50) {
			ypos = 50;
		}
		if (ypos >= 650) {
			ypos = 650;
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		xpos = e.getX() - 50;
		ypos = e.getY() - 50;
		if (xpos <= 50) {
			xpos = 50;
		}
		if (xpos >= 650) {
			xpos = 650;
		}
		if (ypos <= 50) {
			ypos = 50;
		}
		if (ypos >= 650) {
			ypos = 650;
		}
	}
}